﻿using Microsoft.Extensions.Logging;
using PropertyManagment.config;
using PropertyManagment.entity;
using PropertyManagment.entity.dto;
using PropertyManagment.mapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PropertyManagment.service.impl {

    public class SysMenuServiceImpl : ServiceImpl<SysMenuMapper, SysMenu>, SysMenuService {

        public SysMenuMapper sysMenuMapper { get; set; }
        public SysUserMapper sysUserMapper { get; set; }

        public List<SysMenuDto> getCurrentUserNav(string username) {
            //String username = (String)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            SysUser sysUser = sysUserMapper.getAll().Where(u => u.username == username).FirstOrDefault();

            List<long> menuIds = sysMenuMapper.getMenuIdByUserId(sysUser.id);
            List<SysMenu> menus = this.listByIds(menuIds);
            // 转树状结构
            List<SysMenu> menuTree = buildTreeMenu(menus);
            // 过滤掉状态为禁用的目录
            menuTree = filterForbid(menuTree);

            // 实体转DTO
            return convert(menuTree);
        }
        private List<SysMenu> filterForbid(List<SysMenu> menuTree) {
            menuTree = menuTree.Where<SysMenu>(f => f.statu != 0).ToList();
            foreach (SysMenu menu in menuTree) {
                if (menu.children.Count > 0) {
                    menu.children = filterForbid(menu.children);
                }
            }

            return menuTree;
        }

        private List<SysMenuDto> convert(List<SysMenu> menuTree) {
            List<SysMenuDto> menuDtos = new List<SysMenuDto>();

            menuTree.ForEach(m => {
                SysMenuDto dto = new SysMenuDto();
                dto.id = m.id;
                dto.name = m.perms;
                dto.title = m.name;
                dto.component = m.component;
                dto.icon = m.icon;
                dto.path = m.path;
                if (m.children.Count > 0) {
                    dto.children = convert(m.children);
                }
                menuDtos.Add(dto);
            });

            return menuDtos;
        }

        public List<SysMenu> tree() {
            // 获取所有菜单信息
            List<SysMenu> sysMenus = sysMenuMapper.getAll().OrderBy(p => p.orderNum).ToList();
            // 转成树状结构
            return buildTreeMenu(sysMenus);
        }

        private List<SysMenu> buildTreeMenu(List<SysMenu> menus) {
            List<SysMenu> finalMenus = new List<SysMenu>();
            // 先各自寻找到各自的孩子
            foreach (SysMenu menu in menus) {
                foreach (SysMenu e in menus) {
                    if (menu.id == e.parent_id) {
                        menu.children.Add(e);
                    }
                }
                // 提取出父节点
                if (menu.parent_id == 0L) {
                    finalMenus.Add(menu);
                }
            }
            return finalMenus;
        }
    }
}
